package com.sitech.ibnms.c3p.util;


import com.sitech.ismp.util.PropertyPlaceholderConfigurer;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Properties;

/**
 * Created by Administrator on 2017/1/13.
 * 为了同步CSD的统一密码
 */
public class MyBasicDbConfigurer extends PropertyPlaceholderConfigurer {
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    protected void convertProperties(Properties props){
        String userName = props.getProperty("spring.datasource.username");
        String password = props.getProperty("spring.datasource.password");
        logger.info("MyBasicDbConfigurer java class is start...........");
         //调用CSD的接口获取数据库密码
        try {
            if(props.containsKey("spring.datasource.tag")){
                String tag=props.getProperty("spring.datasource.tag");
                logger.info("MyBasicDbConfigurer java tag: "+tag);
                if(null!=tag&&!"".equals(tag)){
                    Map<String,String> passMap= SyscHostDbPasswdUtils.SyscHostDbPasswd(tag);
                    if(userName.equals(passMap.get("DB_USER"))){
                        password=passMap.get("DB_PASSWD");
                        //加密
                        password=Base64.encode(password.getBytes());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("MyBasicDbConfigurer java password: "+password);
        props.setProperty("spring.datasource.password",password);
        if (this.getEncryptList() != null)
        {
            for (String encrypt : this.getEncryptList())
            {
                if (!props.containsKey(encrypt))
                    continue;
                String value = (String)props.get(encrypt);
                value = new String(Base64.decode(value));
                props.setProperty(encrypt, value);
            }
        }
    }
}
